package com.starbaba.module.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.module.model.LoanApiActivity;
import com.starbaba.module.service.ApiActivityService;

@Component
@SuppressWarnings({"rawtypes","deprecation"})
public class ApiActivityServiceImpl implements ApiActivityService{

    static BaseDao dao = new BaseDao(Constant.POOL_LIFE);
    static BaseDao testdao = new BaseDao(Constant.POOL_PUSH_RW);

    public void saveActivity(LoanApiActivity model) throws AppException{
    	dao.add(model);
    }
    
    public void updateActivity(LoanApiActivity model) throws AppException{
    	String updateSql = "UPDATE loan_api_activity SET activity_name = ?,remark = ?,api_module_id = ?,uptime = ? WHERE id = ?";
    	dao.update(updateSql, new Object[]{model.getActivity_name(),model.getRemark(),model.getApi_module_id(),new Date(),model.getId()});
    }
    
    public List getDataList(int iDisplayStart, int iDisplayLength, PageResponse pageResponse, Map<String, Object> paramsMap) {

        String modelName = (String) paramsMap.get("name");
        String status = (String) paramsMap.get("status");
        StringBuffer sf = new StringBuffer();
        StringBuffer tt = new StringBuffer();
        sf.append("SELECT * FROM loan_api_activity ac WHERE 1=1 ");

        if (modelName != null && "".equals(modelName)) {
            tt.append("AND ac.activity_name LIKE '%" + modelName + "%' ");
        }
        
        sf.append(tt.toString());
        sf.append(" ORDER BY ac.ctime DESC " + "LIMIT " + iDisplayStart + "," + iDisplayLength);
        List<LoanApiActivity> list = null;
        try {
            list = dao.list(sf.toString(), LoanApiActivity.class);

            String countHql = "select count(*) FROM loan_api_activity ac WHERE 1=1 " + tt.toString();
            int count = dao.getInt(countHql, new Object[] {});

            pageResponse.setITotalRecords(count);
            pageResponse.setITotalDisplayRecords(count);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return list;
    }
    
    public void updateContext(Map<String, Object> paramsMap) throws AppException{
    	String updateSql = "";
    	Long id = Long.valueOf((String) paramsMap.get("id"));
    	String activityName = (String) paramsMap.get("activityName");
    	String remark = (String) paramsMap.get("remark");
    	if(activityName != null){
    		updateSql = "UPDATE loan_api_activity SET activity_name = '"+activityName+"' WHERE id = ?";
    	}else{
    		updateSql = "UPDATE loan_api_activity SET remark = '"+remark+"' WHERE id = ?";
    	}
    	dao.update(updateSql, new Object[]{id});
    }
    
    
}











